/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package polybook.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import polybook.MainController;
import polybook.model.Network;
import polybook.model.Person;
import polybook.view.InfosDefaultView;
import polybook.view.InfosProgressView;
import polybook.view.PersonJTableModel;

/**
 *
 * @author epulapp
 */
public class NetworkThread extends Thread {

    protected int countPersonToAdd;

    public NetworkThread(int personCounter) {
        super();
        this.countPersonToAdd = personCounter;
    }

    @Override
    public void run() {
        try {
            MainController controller = MainController.getInstance();
            JOptionPane.showMessageDialog(controller, "La fenetre va être verrouillée pour le temps de l'opération. ", "Information", JOptionPane.INFORMATION_MESSAGE);

            controller.lockWindow();

            // Creation de la vue progressView
            InfosProgressView progressView = new InfosProgressView();
            progressView.includeIn(controller.getBaseView().getInfosPanel());

            // Assignement du nouveau réseau courant
            Network network = controller.getCurrentNetwork();
            network.setCallbacker(progressView.getProcessCallback());
            network.addPersonAtNetwork(countPersonToAdd);

            // Refresh de la JTable
            ArrayList<Person> personList = controller.getCurrentNetwork().getContainer();
            PersonJTableModel currentDatasModel = new PersonJTableModel(personList);
            controller.getCurrentTable().setModel(currentDatasModel);

            // MAJ du bloc d'info reseau
            InfosDefaultView infosView = new InfosDefaultView(network);
            infosView.includeIn(controller.getBaseView().getInfosPanel());

            //Unlock de la fenetre
            controller.unlockWindow();

        } catch (IOException ex) {
            Logger.getLogger(NetworkThread.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(NetworkThread.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
